package myblog.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import myblog.dao.UserDao;
import myblog.entity.UserEntity;
import myblog.vo.UsersVo;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;


public class UserDaoImpl extends JdbcDaoSupport implements UserDao {

	@Override
	public List<UserEntity> list() {
		String sql = "select * from users";

		JdbcTemplate jt = getJdbcTemplate();

		List<UserEntity> userList = jt.query(sql, new UserRowMapper());

		return userList;
	}
	
	class UserRowMapper implements RowMapper {
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

			UserEntity entity = new UserEntity();

			entity.setUser_id(rs.getString("user_id"));
			entity.setUser_name(rs.getString("user_name"));
			entity.setUser_pw(rs.getString("user_pw"));
			entity.setUser_question(rs.getString("user_question"));
			entity.setUser_createdate(rs.getString("user_createdate"));
			entity.setUser_answer(rs.getString("user_answer"));
			entity.setUser_sex(rs.getString("user_sex"));
			entity.setUser_content(rs.getString("user_content"));
			entity.setUser_email(rs.getString("user_email"));

			return entity;
		}
	}

	@Override
	public void addUser(UsersVo userVo) {
		JdbcTemplate jt = getJdbcTemplate();

		String sql = "insert into users(user_name,user_pw,user_question,user_answer,user_sex,user_content,user_email) values(?,?,?,?,?,?,?)";

		// 构建参数数组
		Object[] args = { userVo.getUser_name(),userVo.getUser_pw(),userVo.getUser_question(),userVo.getUser_answer(),userVo.getUser_sex(),userVo.getUser_content(),userVo.getUser_email() };

		jt.update(sql, args);
	}

	@Override
	public UserEntity getuser(String user_id) {
		JdbcTemplate jt = getJdbcTemplate();

		String sql = "select * from users where usersid = ?";

		// 构建参数数组
		Object[] args = { user_id };

		UserEntity userEntity = (UserEntity) jt.queryForObject(sql, args,
				new UserRowMapper());

		return userEntity;
	}

	@Override
	public void editUser(UsersVo userVo) {
		JdbcTemplate jt = getJdbcTemplate();

		String sql = "update users set user_name= ?, user_pw = ?,user_question = ?,user_answer = ?,user_sex = ?,users_content = ?,user_email = ? where user_id = ?";

		// 构建参数数组
		Object[] args = { userVo.getUser_name(),userVo.getUser_pw(),userVo.getUser_question(),userVo.getUser_answer(),userVo.getUser_sex(),userVo.getUser_content(),userVo.getUser_email(),userVo.getUser_id() };

		jt.update(sql, args);
		
	}

	@Override
	public void delUser(String user_id) {
		JdbcTemplate jt = getJdbcTemplate();

		String sql = "delete from users where usersid = ?";

		// 构建参数数组
		Object[] args = { user_id };

		jt.update(sql, args);
	}

	@Override
	public UserEntity getuserByName(String username) {
		JdbcTemplate jt = getJdbcTemplate();

		String sql = "select * from users where user_name = ?";

		// 构建参数数组
		Object[] args = { username };

		UserEntity userEntity = (UserEntity) jt.queryForObject(sql, args,
				new UserRowMapper());

		return userEntity;
	}

}
